package com.cmict.data.form.api.mapper;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.mybatis.core.mapper.BaseMapperX;
import com.cmict.data.framework.mybatis.core.query.QueryWrapperX;
import com.cmict.data.form.api.entity.dataobject.SysFormDO;
import com.cmict.data.form.api.entity.vo.SysFormReqVO;
import com.cmict.data.framework.tenant.core.aop.TenantIgnore;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 类别表
 *
 * @author
 * @date 2022-03-21 11:37:08
 */
@Mapper
public interface SysFormMapper extends BaseMapperX<SysFormDO> {

    @Update("UPDATE sys_form SET deleted = 1 WHERE id in #{idList}")
    Boolean batchDeleteSysForm(List<Long> idList);

    @Update("UPDATE sys_form SET is_enable = 0 WHERE id = #{id}")
    Boolean enable(Long id);

    @Update("UPDATE sys_form SET is_enable = 1 WHERE id = #{id}")
    Boolean disable(Long id);

    @Update("UPDATE sys_form SET is_reference = 0 ,use_menu_id = NULL ,manage_menu_id = NULL WHERE id = #{id}")
    Boolean noRelease(Long id);

    default PageResult<SysFormDO> selectPage(SysFormReqVO reqVO) {
        return selectPage(reqVO, new QueryWrapperX<SysFormDO>()
                .likeIfPresent("form_name", reqVO.getFormName())
                .eqIfPresent("category_id",reqVO.getCategoryId())
                .isNull("parent_id")
                .orderByDesc("create_time"));
    }

    default SysFormDO selectOne(String formName){
        return selectOne("form_name", formName);
    }

    default SysFormDO selectOneId(Long id){
        return selectOne("id", id);
    }

    @TenantIgnore
    default SysFormDO selectOneByTableName(String tableName){
        return selectOne("table_name", tableName);
    }

    default PageResult<SysFormDO> selectPageRelease(SysFormReqVO reqVO) {
        return selectPage(reqVO, new QueryWrapperX<SysFormDO>()
                .likeIfPresent("form_name", reqVO.getFormName())
                .eqIfPresent("is_reference",1)
                .eq("is_enable",0)
                .isNull("parent_id")
                .orderByDesc("release_time"));
    }

    default PageResult<SysFormDO> selectPageNoRelease(SysFormReqVO reqVO) {
        return selectPage(reqVO, new QueryWrapperX<SysFormDO>()
                .likeIfPresent("form_name", reqVO.getFormName())
                .eqIfPresent("is_reference",0)
                .eq("is_enable",0)
                .isNull("parent_id")
                .orderByDesc("create_time"));
    }

    default List<SysFormDO> selectListRelease() {
        return selectList(new QueryWrapperX<SysFormDO>()
                .eqIfPresent("is_reference",1)
                //.eq("is_enable",0)
                .orderByDesc("create_time"));
    }
    default List<SysFormDO> selectListReleaseByEncode(String tableName){
        return selectList(new QueryWrapperX<SysFormDO>()
                .eqIfPresent("is_reference",1)
                .ne(StringUtils.isNoneBlank(tableName),"table_name",tableName)
                //.eq("is_enable",0)
                .orderByDesc("create_time"));
    }
    default List<SysFormDO> selectListAll() {
        return selectList(new QueryWrapperX<SysFormDO>()
                //.isNull("parent_id")
                //.eq("is_enable",0)
                .orderByDesc("create_time"));
    }

    default List<SysFormDO> selectListManageMenu(Long manageMenuId) {
        return selectList(new QueryWrapperX<SysFormDO>()
                .eq("manageMenuId",manageMenuId));
    }

    default List<SysFormDO> getChildSysFormByIds(Long parentId) {
        return selectList(new QueryWrapperX<SysFormDO>()
                .eq("parent_id",parentId)
                .eq("deleted",0)
                .orderByDesc("create_time"));
    }


    default PageResult<SysFormDO> getSysFormPageIsenable(SysFormReqVO reqVO) {
        return selectPage(reqVO, new QueryWrapperX<SysFormDO>()
                .likeIfPresent("form_name", reqVO.getFormName())
                .eqIfPresent("category_id",reqVO.getCategoryId())
                .isNull("parent_id")
                .eq("is_enable",0)
                .orderByDesc("create_time"));
    }


    @Select({"select use_menu_id from sys_form where use_menu_id is not null and deleted = 0"})
    Set<Long> getUserMenuId();

    @Select({"select * from sys_form where use_menu_id in (${menuIds})"})
    List<SysFormDO> getSysFormByUseMenuIds(@Param("menuIds") String menuIds);

    @Select({"select manage_menu_id from sys_form where deleted = 0 and manage_menu_id is not null"})
    Set<Long> getManageMenuId();

    @Select({"select * from sys_form where use_menu_id in (${menuIds}) or manage_menu_id in (${menuIds})"})
    Set<SysFormDO> getSysFormByFormMenuIds(@Param("menuIds") String menuIds);

    @Select({"SELECT sf.id as id , sm.name AS formName ,sf.table_name AS  tableName  FROM sys_form sf left join system_menu  sm on sf.manage_menu_id = sm.id  where sf.is_reference = 1 and sf.is_enable = 0 order by sf.create_time desc"})
    List<SysFormDO> selectListReleaseManage();

    @Select({"select * from sys_form where deleted = 0 AND use_menu_id = ${menuIds} OR manage_menu_id = ${menuIds}"})
    SysFormDO getSysFormByMenuId(@Param("menuId") Long menuId);

    @Delete({"delete from sys_form WHERE table_name = '${tableName}'"})
    void deleteFormOfTableName(@Param("tableName") String tableName);

    @Select("${sql}")
    @DS("slave")
    List<Map<String, Object>> getTableNameList(String sql);
}
